package com.iplant.basic.serviceImpl.dao.mss;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.annotation.dao.DBBaseDAO;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.service.po.mss.MSSUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

@DBBaseDAO(MSSUnit.class)
public class MSSUnitDAO extends BaseDAO {

	private static Logger logger = LoggerFactory.getLogger(MSSUnitDAO.class);
	private static MSSUnitDAO Instance = null;

	private MSSUnitDAO() {
		super("");
		this.Init();
	}

	public static MSSUnitDAO getInstance() {
		if (Instance == null)
			Instance = new MSSUnitDAO();
		return Instance;
	}

	public void MSS_UpdateUnit(BMSEmployee wLoginUser, MSSUnit wUnit, OutResult<Integer> wErrorCode) {
		wErrorCode.set(0);

		try {
			if (wUnit == null || StringUtils.isEmpty(wUnit.Name)) {
				wErrorCode.set(ExceptionTypes.Parameter.getValue());
				return;
			}
			MSSUnit wUnitO = this.MSS_CheckUnit(wLoginUser, wUnit, wErrorCode);
			if (wUnitO.ID > 0) {
				wErrorCode.set(ExceptionTypes.Duplication.getValue());
			}
			if (wErrorCode.Result != 0)
				return;

			Map<String, Object> wParams = new HashMap<String, Object>();

			wParams.put("Name", wUnit.Name);
			wParams.put("Code", wUnit.Code);
			wParams.put("Description", wUnit.Description);
			wParams.put("GroupID", wUnit.GroupID);

			wParams.put("EditorID", wLoginUser.getID());
			wParams.put("EditTime", Calendar.getInstance());
			wParams.put("Active", wUnit.Active);

			if (wUnit.ID > 0) {
				wParams.put("ID", wUnit.ID);
				super.Update(super.getTableName("mss_unit"), "ID", wParams);
			} else {
				wParams.put("CreatorID", wLoginUser.getID());
				wParams.put("CreateTime", Calendar.getInstance());
				wUnit.ID = super.Insert(super.getTableName("mss_unit"), wParams);
			}

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("MSS_UpdateUnit", ex);
		}
	}

	public void MSS_ActiveUnit(BMSEmployee wLoginUser, MSSUnit wUnit, int wActive, OutResult<Integer> wErrorCode) {
		wErrorCode.set(0);
		// 判断客户信息是否存在(中国：统一社会信用代码，国外:提醒是否有重复）

		try {
			if (wUnit == null || wUnit.ID <= 0) {
				wErrorCode.set(ExceptionTypes.Parameter.getValue());
				return;
			}

			Map<String, Object> wParams = new HashMap<String, Object>();

			wParams.put("ID", wUnit.ID);
			wParams.put("EditorID", wLoginUser.getID());
			wParams.put("EditTime", Calendar.getInstance());
			wParams.put("Active", wActive);
			super.Update(super.getTableName("mss_unit"), "ID", wParams);

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());

			logger.error("MSS_ActiveUnit", ex);
		}
	}

	public List<MSSUnit> MSS_QueryUnitList(BMSEmployee wLoginUser, int wGroupID, String wGroupFullName, String wName,
			int wActive, Pagination wPagination, OutResult<Integer> wErrorCode) {
		return this.MSS_QueryUnitList(wLoginUser, -1, "", wGroupID, wGroupFullName, wName, wActive, wPagination,
				wErrorCode);
	}

	private List<MSSUnit> MSS_QueryUnitList(BMSEmployee wLoginUser, int wID, String wCode, int wGroupID,
			String wGroupFullName, String wName, int wActive, Pagination wPagination, OutResult<Integer> wErrorCode) {
		List<MSSUnit> wMSSUnitList = new ArrayList<MSSUnit>();
		wErrorCode.set(0);

		try {

			// Step02:查询
			Map<String, Object> wParams = new HashMap<String, Object>();
			String wSQLText = "";
			wSQLText = StringUtils.Format(
					"Select t.*,t2.Name as GroupName,t2.Code as GroupCode, concat(t2.GroupFullName,t2.Name) as GroupFullName,"
							+ " t3.Name as CreatorName ,t4.Name as EditorName  from {0}.mss_unit t  "
							+ " left join {0}.mss_unitgroup t2 on t.GroupID=t2.ID "
							+ " left join {0}.mbs_user t3 on t.CreatorID=t3.ID "
							+ " left join {0}.mbs_user t4 on t.EditorID=t4.ID "
							+ " where (:ID<=0 or  t.ID=:ID) and (:Code =''''  or  t.Code=:Code)"
							+ " AND (:Active<0 or  t.Active=:Active)  AND (:GroupID<=0 or  t.GroupID=:GroupID)  AND (:Name = '''' or  t.Name like :Name) "
							+ " AND (:GroupFullName = '''' or  concat(t2.GroupFullName,t2.Name,''/'') like :GroupFullName)  ",
					super.getDBName());

			wSQLText = this.DMLChange(wSQLText);

			wParams.put("ID", wID);
			wParams.put("Code", wCode);
			wParams.put("GroupID", wGroupID);
			wParams.put("Active", wActive);
			wParams.put("Name", StringUtils.isEmpty(wName) ? "" : ("%" + wName + "%"));
			wParams.put("GroupFullName", StringUtils.isEmpty(wGroupFullName) ? "" : (wGroupFullName + "%"));

			wMSSUnitList = super.QueryForList(wSQLText, wParams, wPagination, MSSUnit.class);

			// Step03:配置系统
		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("MSS_QueryUnitList", ex);
		}
		return wMSSUnitList;
	}

	public MSSUnit MSS_QueryUnit(BMSEmployee wLoginUser, int wID, String wCode, OutResult<Integer> wErrorCode) {
		MSSUnit wResult = new MSSUnit();
		wErrorCode.set(0);

		try {
			List<MSSUnit> wList = null;
			if (wID > 0) {
				wList = this.MSS_QueryUnitList(wLoginUser, wID, "", -1, "", "", -1, Pagination.getDefault(),
						wErrorCode);

			} else if (StringUtils.isNotEmpty(wCode)) {
				wList = this.MSS_QueryUnitList(wLoginUser, -1, wCode, -1, "", "", -1, Pagination.getDefault(),
						wErrorCode);
			}
			if (wList != null && wList.size() > 0) {
				wResult = wList.get(0);
			}
		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("MSS_QueryUnitByID", ex);
		}
		return wResult;
	}

	public MSSUnit MSS_CheckUnit(BMSEmployee wLoginUser, MSSUnit wMSSUnit, OutResult<Integer> wErrorCode) {
		MSSUnit wUnit = new MSSUnit();
		wErrorCode.set(0);

		try {

			// Step0:查询
			Map<String, Object> wParams = new HashMap<String, Object>();
			String wSQLText = StringUtils.Format(
					"Select * from {0}.mss_unit "
							+ " where ID!=:ID and ( Name =:Name or (:Code != '''' and Code=:Code)) ",
					super.getDBName());
			wParams.clear();
			wParams.put("ID", wMSSUnit.ID);
			wParams.put("Name", wMSSUnit.Name);
			wParams.put("Code", wMSSUnit.Code);

			wSQLText = this.DMLChange(wSQLText);
			List<Map<String, Object>> wQueryResultList = super.queryForList(wSQLText, wParams);
			for (Map<String, Object> wSqlDataReader : wQueryResultList) {
				wUnit.ID = StringUtils.parseInt(wSqlDataReader.get("ID"));
				wUnit.Name = StringUtils.parseString(wSqlDataReader.get("Name"));
				wUnit.Code = StringUtils.parseString(wSqlDataReader.get("Code"));

				wUnit.CreatorID = StringUtils.parseInt(wSqlDataReader.get("CreatorID"));
				wUnit.CreateTime = StringUtils.parseCalendar(wSqlDataReader.get("CreateTime"));
				wUnit.EditorID = StringUtils.parseInt(wSqlDataReader.get("EditorID"));
				wUnit.EditTime = StringUtils.parseCalendar(wSqlDataReader.get("EditTime"));
				wUnit.Active = StringUtils.parseInt(wSqlDataReader.get("Active"));

			}

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("MSS_CheckUnit", ex);
		}
		return wUnit;
	}

}